package ai.people.netmon.framework.domain.archives.entity;

import ai.people.netmon.framework.constant.NumberHideConstant;
import ai.people.netmon.framework.domain.archives.vo.ArchivesVO;
import ai.people.netmon.utils.NumberUtils;
import ai.people.netmon.utils.StringPool;
import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 线索表
 *
 * @author zhenglin
 * @since 2022-05-31
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ClueArchives implements Serializable {

    /**
     * 主键ID
     */
    private Long id;

    /**
     * 档案ID
     */
    private Long archivesId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 姓名
     */
    private String name;

    /**
     * 照片
     */
    private String picture;
    /**
     * 身份证
     */
    private String idNumber;

    /**
     * 手机号
     */
    private String phoneNumber;

    /**
     * 车牌号
     */
    private String plateNumber;

    /**
     * 标签
     */
    private String tags;

    /**
     * 个人信息
     */
    private String personalSituation;

    /**
     * 档案研判
     */
    private String archivesJudgment;

    /**
     * 社交媒体
     */
    private String socialInfo;

    /**
     * 电子邮箱
     */
    private String emailInfo;

    private List<ClueShareRecord> shareRecords;
    /**
     * 创建者账号
     */
    private String creatorName;

    public String buildHideIdNumber() {
        return StringUtils.isNotBlank(this.idNumber) ? NumberUtils.hideBit(this.idNumber, NumberHideConstant.ID_HIDE) : null;
    }

    public String buildFirstName() {
        List<String> list = buildName();
        return list.isEmpty() ? null : list.get(0);
    }

    public List<String> buildName() {
        if (StringUtils.isNotBlank(this.name)) {
            return List.of(this.name.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<String> buildPhone() {
        if (StringUtils.isNotBlank(this.phoneNumber)) {
            return List.of(this.phoneNumber.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public List<String> buildPlate() {
        if (StringUtils.isNotBlank(this.plateNumber)) {
            return List.of(this.plateNumber.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public String buildFirstPlateNumber() {
        List<String> list = buildPlate();
        return list.isEmpty() ? null : list.get(0);
    }

    public String buildHidePhone() {
        String phone = buildFirstPhone();
        return StringUtils.isNotBlank(phone) ? NumberUtils.hideBit(phone, NumberHideConstant.PHONE_HIDE) : null;
    }

    public String buildHidePlate() {
        String plateNumber = buildFirstPlateNumber();
        return StringUtils.isNotBlank(plateNumber) ? NumberUtils.hideBit(plateNumber, NumberHideConstant.CAR_HIDE) : null;
    }

    public String buildFirstPhone() {
        List<String> list = buildPhone();
        return list.isEmpty() ? null : list.get(0);
    }

    public List<String> buildTags() {
        if (StringUtils.isNotBlank(this.tags)) {
            return List.of(this.tags.split(StringPool.COMMA));
        }
        return Collections.emptyList();
    }

    public String buildInfo() {
        return StringUtils.isNotBlank(this.archivesJudgment) ? this.archivesJudgment : this.personalSituation;
    }

    public List<Map<String, String>> buildSocialInfo() {
        List<Map<String, String>> list = new ArrayList<>();
        if (StringUtils.isNotBlank(this.socialInfo)) {
            List<ArchivesVO.SocialInfo.SocialInfoInner> socialInfoInners = JSONArray.parseArray(this.socialInfo, ArchivesVO.SocialInfo.SocialInfoInner.class);
            for (ArchivesVO.SocialInfo.SocialInfoInner socialInfoInner : socialInfoInners) {
                String socialPlatform = socialInfoInner.getSocialPlatform();
                String username = socialInfoInner.getUsername();
                list.add(Map.of(socialPlatform, username));
            }
            return list;
        }
        return Collections.emptyList();
    }

    public String buildSharedUsers() {
        if (CollectionUtils.isNotEmpty(shareRecords)) {
            int size = shareRecords.size();

            List<String> collect = shareRecords.stream().map(ClueShareRecord::getUserName).limit(2).collect(Collectors.toList());
            String join = StringUtils.join(collect, StringPool.STOP_SIGN);
            if (size > 2) {
                join += StringPool.STOP_SIGN + StringPool.ELLIPSIS;
            }
            return join;
        }
        return null;
    }

    public String buildSocialEmail(){
        if(StringUtils.isNotBlank(this.emailInfo)){
            List<ArchivesVO.SocialInfo.Email> emails = JSONArray.parseArray(this.emailInfo, ArchivesVO.SocialInfo.Email.class);
            for (ArchivesVO.SocialInfo.Email email : emails) {
                String email1 = email.getEmail();
                if (StringUtils.isNotBlank(email1)){
                    return email1;
                }
            }
        }
        return null;
    }

}
